package cc.telecomdigital.tdstock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.activity.BaseActivity;

public class NetworkingInfoListenerReceiver extends BroadcastReceiver
{
	private final static String	TAG	= NetworkingInfoListenerReceiver.class
										.getSimpleName();

	@Override
	public void onReceive(final Context context, Intent intent)
	{
		TDStockLog.d(TAG, "received the changing of the networkingInfo!");
		TDStockLog.d(TAG, "context : " + context.getClass().getSimpleName());
		boolean  isNetworkingConnected = SystemInfo.isNetworkingConnected(context);
		
		boolean  isLightStreamerConnected = ((ITDLApplication)(context.getApplicationContext())).isLightStreamerConnected();
		
		if(isNetworkingConnected  &&  !isLightStreamerConnected) {
			if(BaseActivity.INSTANCE_FOR_LIGHTSTREAMER_ONLY != null &&  BaseActivity.isLightStreamerActivity(BaseActivity._currActivityClass) ) {
				TDStockLog.d(TAG, "ready to re-connect lightstreamer");
				BaseActivity.INSTANCE_FOR_LIGHTSTREAMER_ONLY .connectLightStreamerExt(BaseActivity.INSTANCE_FOR_LIGHTSTREAMER_ONLY.getParent());
				TDStockLog.d(TAG, "successfully re-connected the  lightstreamer");
			}
			//instance.connectLightStreamerEx(instance.getParent());
		}
		else if(!isNetworkingConnected ) {
			((ITDLApplication)(context.getApplicationContext())).setLightStreamerConnected(false);
			 TDStockLog.d(TAG, "ready to cancel all async task!!");
			 BaseAsyncTask.cancelAllTask();
			/* TDStockLog.d(TAG, "ready to show sunflower!");
			 SunFlowerView2.showInScreenCenter(context);
			 TDStockLog.d(TAG, "successfully showed sunflower!");*/
		}
		else if(isNetworkingConnected) {
			/*TDStockLog.d(TAG, "ready to hide sunflower!");
			 SunFlowerView2.hide(context);
			 TDStockLog.d(TAG, "successfully hided sunflower!");*/
		}
		
		
/*
				BaseActivity  instance = null;
				if(context instanceof BaseActivity )
				{
					instance = (BaseActivity)context;
					
				}
				else {
					TDStockLog.d(TAG, "context is not a instance of BaseActivity! ");
				}
				
				if(instance != null)
				{
					try
					{
						boolean  isNetworkingConnected = SystemInfo.isNetworkingConnected(context);
						
						boolean  isLightStreamerConnected = ((ITDLApplication)(context.getApplicationContext())).isLightStreamerConnected();
						
						if(isNetworkingConnected  &&  !isLightStreamerConnected) {
							instance.connectLightStreamerEx(instance.getParent());
						}
						else if(!isNetworkingConnected ) {
							
							 BaseAsyncTask.cancelAllTask();
							 SunFlowerView2.showInScreenCenter(context);
						}
						else if(isNetworkingConnected) {
							 SunFlowerView2.hide(context);
						}
						
						
					
					}catch(Exception e)
					{
						
					}
					
				}*/

			}


}
